#include "TitleScene.h"
#include "LevelSelectScene.h"

using namespace cocos2d;

CCScene* TitleScene::scene(){
	CCScene * scene = NULL;
	do 
	{
		// 'scene' is an autorelease object
		scene = CCScene::create();
		CC_BREAK_IF(! scene);

		// 'layer' is an autorelease object
		TitleScene *layer = TitleScene::create();
		CC_BREAK_IF(! layer);

		// add layer as a child to scene
		scene->addChild(layer);
	} while (0);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool TitleScene::init()
{
	bool bRet = false;
	do 
	{
		//////////////////////////////////////////////////////////////////////////
		// super init first
		//////////////////////////////////////////////////////////////////////////

		CC_BREAK_IF(! CCLayer::init());

		//////////////////////////////////////////////////////////////////////////
		// add your codes below...
		//////////////////////////////////////////////////////////////////////////

		CCSize size = CCDirector::sharedDirector()->getWinSize();

		// 1. Add a menu item with "X" image, which is clicked to quit the program.

		// Create a "close" menu item with close icon, it's an auto release object.
		// Place the menu item bottom-right conner.
		CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
			"CloseNormal.png",
			"CloseSelected.png",
			this,
			menu_selector(TitleScene::menuCloseCallback));
		CC_BREAK_IF(! pCloseItem);

		pCloseItem->setPosition(ccp(size.width - 40, size.height-40));

		// 2. Add a label shows "Hello World".
		// Place the menu item bottom-right conner.
		CCMenuItemImage *pStartItem = CCMenuItemImage::create(
			"images/interface/LogoLine.png",
			"images/interface/LogoLineNormal.png",
			this,
			menu_selector(TitleScene::menuStartCallback));
		CC_BREAK_IF(! pStartItem);

		pStartItem->setPosition(ccp(size.width / 2 -120 , size.height*0.2f));

		// Create a menu with the "close" menu item, it's an auto release object.
		CCMenu* pMenu = CCMenu::create(pCloseItem,pStartItem, NULL);
		pMenu->setPosition(CCPointZero);
		CC_BREAK_IF(! pMenu);

		// Add the menu to HelloWorld layer as a child layer.
		this->addChild(pMenu, 1);

		std::string titleStr = "START!!";
		#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
				//GBKToUTF8(titleStr,"gb2312","utf-8");
		#endif

		CCLabelTTF* pLabel = CCLabelTTF::create(titleStr.c_str(), "Thonburi", 30);
		CC_BREAK_IF(! pLabel);
		pLabel->setColor(ccWHITE);

		// Get window size and place the label upper. 
		pLabel->setPosition(ccp(size.width / 2 -120, size.height*0.2f));

		// Add the label to TitleScene layer as a child layer.
		this->addChild(pLabel, 1);


		// 3. Add add a splash screen, show the cocos2d splash image.
		CCSprite* pSprite = CCSprite::create("images/Logo.jpg");
		CC_BREAK_IF(! pSprite);

		// Place the sprite on the center of the screen
		pSprite->setPosition(ccp(size.width/2, size.height/2));

		// Add the sprite to HelloWorld layer as a child layer.
		this->addChild(pSprite, 0);

		bRet = true;

		CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("music/UraniwaniZombiesGa.mp3", true);

	} while (0);

	return bRet;
}

void TitleScene::menuCloseCallback(CCObject* pSender)
{
	// "close" menu item clicked
	CCDirector::sharedDirector()->end();
}

void TitleScene::menuStartCallback(CCObject* pSender)
{
	CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::create(0.2f, LevelSelectScene::scene()));
}

